Ti.include('util.js', 'styles.js', 'split_view.js', 'services.js', 'order_pdf_win.js');

// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Ti.UI.setBackgroundColor(styles.backgroundColor);

var indWin = Titanium.UI.createWindow();

var indView = Ti.UI.createView({
	backgroundColor:'#000',
	opacity:0.7
});
indWin.add(indView);

var actInd = Ti.UI.createActivityIndicator({
	style:Ti.UI.iPhone.ActivityIndicatorStyle.BIG
});
indWin.add(actInd);

var message = Titanium.UI.createLabel({
	text:"Chargement...",
	color:'#fff',
	width:'auto',
	height:'auto',
	font:{fontSize:20, fontWeight:'bold'},
	bottom:20
});
indWin.add(message);

function showActInd() {
	indWin.add(message);
	actInd.show();
	indWin.open();
}

function hideActInd() {
	indWin.close({
		opacity:0,
		duration:500
	});
}


LoginView = {};

LoginView.win = Ti.UI.createWindow({
	backgroundGradient:{
		type:'linear',
		colors:['B15802', 'white'],
		startPoint:{x:0, y:0},
		endPoint:{x:800, y:1100}
	},
	orientationModes:[
		Ti.UI.LANDSCAPE_LEFT,
		Ti.UI.LANDSCAPE_RIGHT
	]
});
Ti.UI.orientation = Titanium.UI.LANDSCAPE_LEFT;

function login() {
	Services.login(LoginView.tfCode.value, function(user) {
		if (user) {
			SplitView.open(user);
			LoginView.tfCode.color = 'black';
		} else {
			LoginView.tfCode.color = 'red';
			LoginView.tfCode.focus();
		}
	});
}

var borderLogo = Ti.UI.createView({
	width:420,
	height:104,
	top:130,
	borderRadius:10,
	opacity:0.3,
	backgroundGradient:{
		type:'linear',
		startPoint:{x:0, y:0},
		endPoint:{x:420, y:104},
		colors:['white', 'black']
	}
});
LoginView.win.add(borderLogo);

var logo = Ti.UI.createImageView({
	image:'images/tecnica.png',
	width:borderLogo.width - 20,
	height:borderLogo.height - 20,
	top:borderLogo.top + 10,
	borderRadius:10,
	borderWidth:1,
	borderColor:'EBECEB'
});
LoginView.win.add(logo);

var lblTitle = Ti.UI.createLabel({
	text:'Veuillez entrer votre code représentant :',
	color:'black',
	width:'auto',
	height:'auto',
	top:bottom(logo) + 60,
	font:{fontSize:20, fontWeight:'bold'}
});
LoginView.win.add(lblTitle);

var view = Ti.UI.createView({
	width:'auto',
	height:'auto',
	top:bottom(lblTitle) + 20
});
LoginView.win.add(view);

LoginView.tfCode = Ti.UI.createTextField({
	hintText:'Code',
	keyboardType:Ti.UI.KEYBOARD_NUMBER_PAD,
	returnKeyType:Ti.UI.RETURNKEY_GO,
	height:40,
	width:100,
	borderStyle:Ti.UI.INPUT_BORDERSTYLE_ROUNDED
});
LoginView.tfCode.addEventListener('return', function() {
	login();
});
LoginView.tfCode.addEventListener('change', function() {
	if (LoginView.tfCode.value == '') {
		LoginView.tfCode.color = 'black';
	}
});
view.add(LoginView.tfCode);

LoginView.btLogin = Ti.UI.createButton({
	style:Titanium.UI.iPhone.SystemButton.DISCLOSURE,
	width:30,
	height:30,
	top:LoginView.tfCode.top,
	left:LoginView.tfCode.width + 50
});
LoginView.btLogin.addEventListener('click', function(e) {
	login();
});
view.add(LoginView.btLogin);

LoginView.win.addEventListener('open', function(e) {
	LoginView.tfCode.focus();
});


/*----------------------------------------------------------*/
var test = 1;
if (test == 1) {
	LoginView.win.open({transition:Ti.UI.iPhone.AnimationStyle.CURL_UP});
	
	// pour capturer la page d'accueil
	/*LoginView.win.open();
	var f = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory + '/iphone', 'Default-Landscape.png');
	Titanium.Media.takeScreenshot(function(event) {
		f.write(event.media);
	});*/
	
} else if (test == 2) {
	TailleEditWin.open(null, {
		article:{
			ref:'001',
			marque:'VO',
			saison:'H',
			annee:'2011',
			designation:'Désignation de l article',
			couleur:'rouge',
			qte:'10',
			div:'ski',
			hasChild:true
		},
		annee:2011,
		saison:'H',
		taille:200,
		marque:'voelkl',
		couleur:'rouge',
		qte:100,
		qteAnnulee:0,
		prix:1000,
		discount:10,
		prixNet:900
	}).open();
} else if (test == 3) {
	OrderWin.open(51875);
}
